unit uScreen;

interface

uses
  ExtCtrls,
  uTile, uObject, uMap;

type
  TGameScreen = class(TImage)
  private
    { Private declarations }
    images: Array[0..16, 0..15] of TImage;
  public
    { Public declarations }
    useMap: TGameMap;
    procedure addTile(room: Integer; x, y: Integer; tile: TGameTile);
    procedure addObject(room: Integer; x: Integer; y: Integer);
    procedure showMap(room: Integer; x, y: Integer);
    procedure deleteTile(room: Integer; x, y: Integer);
    procedure deleteObject(room: Integer; x, y: Integer);
  end;

implementation

uses
  uMapEditor, Graphics, uObjectOptions;

procedure TGameScreen.addTile(room: Integer; x: Integer; y: Integer; tile: TGameTile);
begin
  x := x-1;
  useMap.rooms[room].tiles[x, y] := tile;
  if ((x - offsetX >= 0) and (x - offsetX < round(self.Width/TileSize)) and
     (y - offsetY >= 0) and (y - offsetY < round(self.Height/TileSize))) then
    begin
      if (((x - offsetX) < 0) or ((x - offsetX) > high(self.images))) then exit;
      if (((y - offsetY) < 0) or ((y - offsetY) > high(self.images[(x - offsetX)]))) then exit;
      self.images[(x - offsetX), (y - offsetY)].Picture.Bitmap := useMap.rooms[room].tiles[x, y].tileSet;
      if ((useMap.rooms[room].objects[x, y].objectType <> '') and
         (useMap.rooms[room].objects[x, y].objectType <> 'not')) then
        begin
          self.images[(x - offsetX), (y - offsetY)].Picture.Bitmap := useMap.rooms[room].objects[x, y].texture;
          self.images[(x - offsetX), (y - offsetY)].Canvas.Rectangle(2, 2, 6, 6);
        end;
    end;
end;

procedure TGameScreen.addObject(room: Integer; x: Integer; y: Integer);
begin
  x := x-1;
  GameObjectOptions.obj := useMap.rooms[room].objects[x, y];
  if (useMap.rooms[room].objects[x, y].objectType = '') then
    begin
      useMap.rooms[room].objects[x, y].objectType := 'something';
    end;
  if ((x - offsetX >= 0) and (x - offsetX < round(self.Width/TileSize)) and
     (y - offsetY >= 0) and (y - offsetY < round(self.Height/TileSize))) then
    begin
      if ((useMap.rooms[room].objects[x, y].objectType <> '') and
         (useMap.rooms[room].objects[x, y].objectType <> 'not')) then
        begin
          self.images[(x - offsetX), (y - offsetY)].Picture.Bitmap := useMap.rooms[room].objects[x, y].texture;
          self.images[(x - offsetX), (y - offsetY)].Canvas.Rectangle(2, 2, 6, 6);
        end;
    end;
end;

procedure TGameScreen.showMap(room: Integer; x, y: Integer);
var
  i, j: Integer;
begin
  if (room <= high(useMap.rooms)) and (room >= low(useMap.rooms)) then
    begin
      for i := 0 to 16 do
        begin
          for j := 0 to 15 do
            begin
              if (self.images[i, j] = nil) then
                begin
                  self.images[i, j] := TImage.Create(self.Owner);
                  self.images[i, j].Width := TileSize;
                  self.images[i, j].Height := TileSize;
                  self.images[i, j].Enabled := true;
                  self.images[i, j].Visible := true;
                  self.images[i, j].OnMouseDown := self.OnMouseDown;
                  self.images[i, j].OnDblClick := self.OnDblClick;
                  self.images[i, j].Parent := self.Parent;
                  self.Visible := false;
                  self.Enabled := false;
                end;
              if(self.images[i, j].Visible = false) then
                begin
                  self.images[i, j].Visible := true;
                end;
              self.images[i, j].Left := self.Left + i*TileSize;
              self.images[i, j].Top := self.Top + j*TileSize;
              if ((x+i >= low(useMap.rooms[room].tiles)) and
                 (x+i <= high(useMap.rooms[room].tiles))) then
                begin
                  if ((y+j >= low(useMap.rooms[room].tiles[x+i])) and
                     (y+j <= high(useMap.rooms[room].tiles[x+i]))) then
                    begin
                      if ((useMap.rooms[room].objects[x+i, y+j].objectType = '') or
                         (useMap.rooms[room].objects[x+i, y+j].objectType = 'not') or
                         (useMap.rooms[room].objects[x+i, y+j].objectType = 'invisible')) then
                        begin
                          self.images[i, j].Picture.Bitmap := useMap.rooms[room].tiles[x+i, y+j].tileSet;
                        end;
                    end;
                end;
              if ((x+i >= low(useMap.rooms[room].objects)) and
                 (x+i <= high(useMap.rooms[room].objects))) then
                begin
                  if ((y+j >= low(useMap.rooms[room].objects[x+i])) and
                     (y+j <= high(useMap.rooms[room].objects[x+i]))) then
                    begin
                      if ((useMap.rooms[room].objects[x+i, y+j].objectType <> '') and
                         (useMap.rooms[room].objects[x+i, y+j].objectType <> 'not')) then
                        begin
                          self.images[i, j].Picture.Bitmap := useMap.rooms[room].objects[x+i, y+j].texture;
                          self.images[i, j].Canvas.Rectangle(2, 2, 6, 6);
                        end;
                    end;
                end;
            end;
        end;
    end;
end;

procedure TGameScreen.deleteTile(room: Integer; x: Integer; y: Integer);
begin
  useMap.rooms[room].tiles[x, y].isWater := false;
  useMap.rooms[room].tiles[x, y].isGorge := false;
  useMap.rooms[room].tiles[x, y].isGhosty := false;
  useMap.rooms[room].tiles[x, y].tileTexture := 0;
  useMap.rooms[room].tiles[x, y].transparent := false;
  useMap.rooms[room].tiles[x, y].tileSet := useMap.tileSets[0];
end;

procedure TGameScreen.deleteObject(room: Integer; x: Integer; y: Integer);
begin
  useMap.rooms[room].objects[x, y].objectType := 'not';
  useMap.rooms[room].objects[x, y].isLiving := false;
  useMap.rooms[room].objects[x, y].texture := useMap.tileSets[0];
  useMap.rooms[room].objects[x, y].texturePosition := 0;
end;

end.
